﻿//设计模式 - 装饰器模式
//目的：重用功能并增加新功能

new Anchor().Inner();
Console.WriteLine();

new Bold(new Anchor()).Inner();
Console.WriteLine();

new Italic(new Bold(new Anchor())).Inner();
Console.WriteLine("\n\n");

new Paragraph().Inner();
Console.WriteLine();

new Bold(new Paragraph()).Inner();
Console.WriteLine();

new Italic(new Bold(new Paragraph())).Inner();
Console.WriteLine();

Console.ReadKey();


abstract class HtmlElement
{
    protected internal abstract void Inner();

    protected abstract string TagName { get; }
}

class Anchor : HtmlElement
{
    protected override string TagName => "a";

    protected internal override void Inner()
    {
        Console.Write("<{0}>Anchor</{0}>", TagName);
    }
}

class Paragraph : HtmlElement
{
    protected override string TagName => "p";

    protected internal override void Inner()
    {
        Console.Write("<{0}>Paragraph</{0}>", TagName);
    }
}

abstract class Decorator : HtmlElement
{
    private readonly HtmlElement _element;

    public Decorator(HtmlElement element)
    {
        this._element = element;
    }

    protected internal HtmlElement Element => _element;

    protected internal override void Inner()
    {
        Console.Write($"<{TagName}>");
        Element.Inner();
        Console.Write($"</{TagName}>");
    }
}

class Bold : Decorator
{
    public Bold(HtmlElement element) : base(element)
    {
    }
    protected override string TagName => "b";

    protected internal override void Inner()
    {
        Console.WriteLine("<<{0}>>xxxxx<</{0}>>", TagName);
        Element.Inner();
    }
}

class Italic : Decorator
{
    public Italic(HtmlElement element) : base(element)
    {
    }
    protected override string TagName => "i";
}